
$(function () {
    initEvent();
});

function initEvent() {
    $("#contactMsg").off('click').on('click', function () {
        contactMsg();
    });
}

// 提交联系人信息
function contactMsg() {
    var contactInfo1 = common.formatFormData('contactInfo1');
    if (!checkParams(contactInfo1))
        return false;
    var contactInfo2 = common.formatFormData('contactInfo2');
    if (!checkParams(contactInfo2))
        return false;
    
    var relationArr = [];
    var phonearr = [];
    var flag = true;
    var nflag = true;
    for (var i = 1; i <= 2; i++) {
        var relation = $("#relation" + i).val();
        var phone = $("#phone" + i).val();
        var name = $("#name" + i).val();
        common.checkForm().checkName(name, function (result, error) {
            if (result == false) {
                falert(error);
                nflag = false;
                return false;
            }else{
                nflag = true;
            }
        });
        common.checkForm().checkMobile(phone, function (result, error) {
            if (result == false) {
                falert(error);
                flag = false;
                return false;
            }else{
                flag = true;
            }
        });
        if (phone) {
            phonearr.push(phone);
        }
        if (relation == '父亲' || relation == '母亲' || relation == '配偶') {
            relationArr.push(relation);
        }
    }
    if(!flag || !nflag){
        return false;
    }
    if (/(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f" + relationArr.join("\x0f\x0f") + "\x0f")) {
        falert('两个紧急联系人不能重复');
        return false;
    }
    if (/(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f" + phonearr.join("\x0f\x0f") + "\x0f")) {
        falert('手机号不能重复');
        return false;
    }
    
    var arr = [];
    arr.push(contactInfo1);
    arr.push(contactInfo2);
    var datas = new Object;
    datas.relations = arr;
    datas.interface = 'users@doContact';
    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                falert('保存成功','/user/msg');
            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });
}